package mediator.chat;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstChatMediator {

	/** 所有在聊天室的人 */
	List<UserColleague> userList = new ArrayList<>();

	/** 群发 */
	abstract void sendToAll(String msg);

	/** 给某人发消息 */
	abstract void sendToPerson(String msg, String name);

	/** 用户加入聊天室 */
	abstract void join(UserColleague user);

	/** 用户离开聊天室 */
	abstract void leave(UserColleague user);
}

class ChatMediator extends AbstChatMediator {

	@Override
	void sendToAll(String msg) {
		for (UserColleague user : this.userList) {
			user.accept(msg);
		}
	}

	@Override
	void sendToPerson(String msg, String name) {
		for (UserColleague user : this.userList) {
			if (name.equals(user.name)) {
				user.accept(msg);
			}
		}

	}

	@Override
	void join(UserColleague user) {
		this.userList.add(user);
	}

	@Override
	void leave(UserColleague user) {
		this.userList.remove(user);
	}

}
